package com.atguigu.three;

import com.atguigu.util.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author gan
 * @create 2021-07-03 16:08
 */
public class Work03 {
    //队列名称
    private static final String ACK_QUEUE_NAME = "ack_queue";

    //接收消息
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();

        System.out.println("c1等待接收消息处理时间较短");

        //消费消息的时候如何处理信息
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");

            SleepUtils.sleep(1);

            System.out.println("接收到的消息：" + message);

            /**
             * 1、消息标记tag
             * 2、false代表只应答接收到的那个传递的消息 true为应答所有消息包括传递过来的消息
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        };

        //设置不公平分发
        //int prefetchCount = 1;
        //设置与取值
        int prefetchCount = 2;
        channel.basicQos(prefetchCount);

        //手动应答
        boolean autoAck = false;
        channel.basicConsume(ACK_QUEUE_NAME, autoAck, deliverCallback, (consumerTag) -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        });
    }
}
